package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：确认订单
 */
public class XFDConfirmOrderAdapter extends RecyclerView.Adapter<XFDConfirmOrderAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<ConfirmGoodBeen.DataBean.CartInfoBean> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;


    public XFDConfirmOrderAdapter(Context context, List<ConfirmGoodBeen.DataBean.CartInfoBean> mList) {
        this.mList = mList;
        this.mContext = context;

        mLayoutInflater = LayoutInflater.from(mContext);
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.item_confirm_order, parent, false);

        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder,   int position) {
        if (holder.ettNote.getTag() instanceof TextWatcher) {
            holder.ettNote.removeTextChangedListener((TextWatcher) holder.ettNote.getTag());
        }
        holder.tvOrderShop.setText(mList.get(position).getStore().getName());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.rvOrderGoods.setLayoutManager(linearLayoutManager);
        holder.rvOrderGoods.setItemAnimator(new DefaultItemAnimator());
        holder.rvOrderGoods.setNestedScrollingEnabled(false);
//        holder.rvOrderGoods.setFocusableInTouchMode(false);
        XFDConfirmOrderGoodsAdapter xfdConfirmOrderGoodsAdapter = new XFDConfirmOrderGoodsAdapter(mContext, mList.get(position).getGoods());
        holder.rvOrderGoods.setAdapter(xfdConfirmOrderGoodsAdapter);
        //单个运费隐藏
//        holder.tvFreight.setText("¥ "+mList.get(position).getPriceGroup().getStorePostage());
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().isEmpty()) {
                    mList.get(position).getPriceGroup().setNote("");
                } else {
                    String num =  holder.ettNote.getText().toString();
                    mList.get(position).getPriceGroup().setNote(num);
//                    holder.ettNote.setSelection(holder.ettNote.getText().length());
                }
            }
        };
        holder.ettNote.addTextChangedListener(textWatcher);

        holder.ettNote.setTag(textWatcher);
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.tv_order_shop)
        TextView tvOrderShop;
        @BindView(R.id.rv_order_goods)
        RecyclerView rvOrderGoods;
//        @BindView(R.id.tv_freight)
//        TextView tvFreight;
        @BindView(R.id.ett_note)
        EditText ettNote;



        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
